home *** CD-ROM | disk | FTP | other *** search
- package com.commerceone.util.identity;
-
- import java.net.InetAddress;
-
- public class LongId implements Identity {
- private static final byte[] s_hostaddr = getHostAddress();
- private static long s_count = 0L;
- private long m_id = 0L;
-
- public String toString() {
- return "" + this.m_id;
- }
-
- public int hashCode() {
- Long obj_id = new Long(this.m_id);
- return obj_id.intValue();
- }
-
- public static LongId create() {
- long id = s_count++ & 65535L;
- id |= (long)(s_hostaddr[3] << 16);
- id |= (long)(s_hostaddr[2] << 24);
- return new LongId(id);
- }
-
- private LongId(long id) {
- this.m_id = id;
- }
-
- private static byte[] getHostAddress() {
- try {
- return InetAddress.getLocalHost().getAddress();
- } catch (Exception var1) {
- return new byte[4];
- }
- }
-
- public static LongId internalize(long id) {
- return new LongId(id);
- }
-
- public static void main(String[] args) {
- for(int i = 0; i < 100; ++i) {
- byte[] bytes = create().toBytes();
- System.out.println(bytes[0] + " " + bytes[1] + " " + bytes[2] + " " + bytes[3]);
- }
-
- }
-
- public boolean equals(Object obj) {
- if (!(obj instanceof LongId)) {
- return false;
- } else {
- return this.m_id == ((LongId)obj).m_id;
- }
- }
-
- public byte[] toBytes() {
- long value = this.m_id;
- byte[] bytes = new byte[4];
- bytes[0] = (byte)((int)(value >> 24 & 255L));
- bytes[1] = (byte)((int)(value >> 16 & 255L));
- bytes[2] = (byte)((int)(value >> 8 & 255L));
- bytes[3] = (byte)((int)(value & 255L));
- return bytes;
- }
-
- public int formatHint() {
- return 1;
- }
- }
-